package cn.shop.merchant.controller;

import cn.dev33.satoken.stp.StpUtil;
import cn.shop.merchant.service.GameInfoService;
import cn.shop.merchant.service.GamePartitionService;
import cn.shop.thiredparty.config.OSSConfig;
import cn.shop.thiredparty.entity.Metadata;
import cn.shop.thiredparty.service.AliOssService;
import cn.shop.utils.ApiResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.io.InputStream;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import static cn.shop.utils.constant.Constant.UID;

/**
 * 通用接口
 *
 * @author : 李一涵
 * @date : Created in 2022-2-26 10:36
 * @description :
 */
@RestController
@RequestMapping("/common")
public class CommonController {

    @Resource
    private GameInfoService gameInfoService;
    @Resource
    private GamePartitionService gamePartitionService;
    @Resource
    private AliOssService ossService;
    @Resource
    private OSSConfig config;

    /**
     * 获取所有游戏
     *
     * @return 返回结果
     */
    @GetMapping("/game")
    public ApiResult<List<Map<String, Object>>> gameList() {
        long uid = StpUtil.getSession().getLong(UID);
        List<Map<String, Object>> data = gameInfoService.allList(uid);
        return ApiResult.success(data);
    }

    /**
     * 获取游戏下所有分区
     *
     * @param id 游戏编号
     * @return 返回结果
     */
    @GetMapping("/partition/{id}")
    public ApiResult<List<Map<String, Object>>> partitionList(@PathVariable long id) {
        List<Map<String, Object>> data = gamePartitionService.allListByGameId(id);
        return ApiResult.success(data);
    }

    @GetMapping("/allpartition")
    public ApiResult<List<Map<String, Object>>> allPartitionList() {
        long uid = StpUtil.getSession().getLong(UID);
        List<Map<String, Object>> data = gamePartitionService.allListByGame(uid);
        return ApiResult.success(data);
    }

    /**
     * 上传图片接口
     *
     * @param files 要上传的图片
     * @return 返回结果
     */
    @PostMapping("/upload")
    public ApiResult<List<Map<String, String>>> upload(@RequestParam MultipartFile[] files){
        List<Map<String, String>> list = new LinkedList<>();
        for(MultipartFile file : files){
            Map<String, String> model = new LinkedHashMap<>();
            //不是图片的处理
            if(!isImage(file.getOriginalFilename())){
                model.put("flag","false");
                model.put("msg", "文件不是图片");
                model.put("file", file.getOriginalFilename());
                list.add(model);
                continue;
            }

            //图片大小不超过5MB
            if(file.getSize()/1024/1024 >= 5){
                model.put("flag","false");
                model.put("msg", "图片大小不能超过5M");
                model.put("file", file.getOriginalFilename());
                list.add(model);
                continue;
            }
            Metadata metadata = new Metadata();
            metadata.setFilename(file.getOriginalFilename());
            metadata.setContentType(file.getContentType());
            InputStream inputStream = null;
            try {
                inputStream = file.getInputStream();
            } catch (IOException e) {
                e.printStackTrace();
            }

            String filename = ossService.upload(metadata, inputStream);
            model.put("flag","true");
            model.put("filename", filename);
            model.put("fileUrl", config.getPrefix() + "/" + filename);
            list.add(model);
        }
        return ApiResult.success(list);
    }

    private boolean isImage(String originalFilename) {
        if (originalFilename == null)
            return false;
        if(originalFilename.contains(".")){
            String fileExt = originalFilename.substring(originalFilename.lastIndexOf("."));
            fileExt = fileExt.toLowerCase();
            return fileExt.equals(".jpg")
                    || fileExt.equals(".jpeg")
                    || fileExt.equals(".gif")
                    || fileExt.equals(".png")
                    || fileExt.equals(".bmp");
        }
        return false;
    }
}
